package com.atguigu.springcloud.payment.controller;

import com.atguigu.springcloud.framework.common.api.ApiResult;
import com.atguigu.springcloud.framework.core.bean.RequestDetail;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lenovo
 * @version 1.0
 * @date 2020/11/11 11:07
 */
@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class PaymentController {

    @Value("${server.port}")
    private Integer port;


    static Map<Integer, RequestDetail> deviceInfoMap;

    static {
        deviceInfoMap = new HashMap<>();
        deviceInfoMap.put(1, new RequestDetail().setIp("1").setPath("Aaaaaaa"));
        deviceInfoMap.put(2, new RequestDetail().setIp("2").setPath("BBBBBBBBBBB"));
        deviceInfoMap.put(3, new RequestDetail().setIp("3").setPath("CccccCCCccc"));
    }

    @GetMapping("payment/nacos/{id}")
    public ApiResult<RequestDetail> getPayment(@PathVariable("id") Integer id) {
        if (id > 100) {
            throw new NumberFormatException("id>100，服务提供方模拟一个异常，方便服务调用方测试！！！");
        }
        return ApiResult.ok(deviceInfoMap.get(id)).setMessage("port=" + port);
    }

}
